// component/player/player.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    isShow: {
      type: Boolean,
      value: true,
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    is_playing:0,//是否正在播放
    animate_timer:null,//定时器
    current_time:0,//播放的位置  毫秒
    total_time:12.66,//总时间  单位秒
    voice_url:null,
    animationData: {},
    play_icon: 'icon-bofang',
  },

  /**
   * 组件的方法列表
   */
  methods: {
    startGo:function(){
      var animation = wx.createAnimation({
        timingFunction: 'linear',
        duration:150,
      })

      this.animation = animation;
      
      if (this.data.animate_timer != null) {
        clearInterval(this.data.animate_timer);
        this.setData({
          animate_timer: null,
          play_icon: 'icon-bofang'
        })
        return false;
      } else {
        if (this.data.current_time >= 1000 * this.data.total_time){
          this.setData({
            current_time: 0,
          });
          animation.width(this.data.current_time / 1000 / this.data.total_time * 100 + '%').step();
        }
        var time=200;//200毫秒
        var timer = setInterval(function () {
          
          if (this.data.current_time >= 1000 * this.data.total_time){
            clearInterval(this.data.animate_timer);
            this.setData({
              animate_timer: null,
              play_icon: 'icon-bofang',
            })
          }else{
            this.setData({
              current_time: this.data.current_time + time,
            });
          }
          this.setData({
            animationData: animation.export(),
          });
          if (this.data.current_time + time >= 1000 * this.data.total_time){
            this.setData({
              current_time: this.data.total_time * 1000
            });
          }
          animation.width(this.data.current_time / 1000 / this.data.total_time * 100 + '%').step();
        }.bind(this), time); //10毫秒更新一次

        this.setData({
          animate_timer: timer,
          play_icon:'icon-pause'
        });
      }
    }
  }
})
